using System;
using System.Xml;
using Xunit.Sdk;
using Xunit;

namespace SubSpec
{
    internal class FixedResultCommand : ITestCommand
    {
        private readonly IMethodInfo _method;
        private readonly MethodResult _result;
        
        public FixedResultCommand( IMethodInfo method,  MethodResult result )
        {
            _method = method;
            _result = result;
        }

        public bool ShouldCreateInstance
        {
            get { return false; }
        }

        public MethodResult Execute( object testClass )
        {
            return _result;
        }

        public string DisplayName
        {
            get { return _result.DisplayName; }
        }

        public int Timeout
        {
            get { return 0; }
        }

        public virtual XmlNode ToStartXml()
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml( "<dummy/>" );
            XmlNode testNode = XmlUtility.AddElement( doc.ChildNodes[0], "start" );

            string typeName = _method.TypeName;
            string methodName = _method.Name;

            XmlUtility.AddAttribute( testNode, "name", typeName + "." + methodName );
            XmlUtility.AddAttribute( testNode, "type", typeName );
            XmlUtility.AddAttribute( testNode, "method", methodName );

            return testNode;
        }
    }

    internal class ExceptionTestCommand : FixedResultCommand
    {
        public ExceptionTestCommand( IMethodInfo method, Exception exception )
            : base( method, new FailedResult( method, exception, null) )
        { }
    }
}